<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: admincp_addons.php 17341 2010-10-09 12:15:34Z monkey $
 */

if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
		exit('Access Denied');
}

define('ADDONS_SERVER', 'http://addons.discuz.com');

cpheader();
?>
<script src="static/js/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="static/js/jquery-ui-1.8.10.custom.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="static/css/jquery-ui-1.8.10.custom.css" />
<style>
td{
	text-align:left;
}
</style>
<script>
$(function() {
	$( "#dialog" ).dialog({
		autoOpen: false,
		width:300,
		title:"添加论坛首页图片"
	});
	$( "#opener" ).click(function() {
		$( "#dialog" ).dialog( "open" );
		return false;
	});
});
</script>
<?php 
if(!$operation) {
	echo '<table width="100%"  border="0" cellpadding="2" cellspacing="1"  bgcolor="#C0C0C0">';
	echo '<tr bgcolor="#dbeaf5"><th bgcolor="#dbeaf5">标题</th><th bgcolor="#dbeaf5">图片</th><th bgcolor="#dbeaf5">时间</th><th bgcolor="#dbeaf5">操作<a href="javascript:void(0);" id="opener">[添加]</a></th></tr>';
	$query = DB::query("SELECT * FROM ".DB::table('f_imgposts')." f ");
	while($imgposts = DB::fetch($query)) {
		echo "<tr style='background-color:#FFFFFF;'><td><a href='{$imgposts['link']}' target='_bank'>{$imgposts['title']}</a></td><td>{$imgposts['pic']}</td><td>{$imgposts['tm']}</td><td><a href='admin.php?action=imgposts&operation=remove&id={$imgposts['id']}'>删除</a></td></tr>";	
	}	
	echo '</table>';	
	echo '
	<div style="display:none;" id="dialog">
	<form method="post"  action="admin.php?action=imgposts&operation=add" enctype="multipart/form-data">
	<table width="100%"  border="0" cellpadding="2" cellspacing="1"  bgcolor="#C0C0C0">
	<tr style="background-color:#FFFFFF;">
		<td style="width:120px;">标题</td>
		<td><input type="text" name="title" id="title" style="width:200px;"></td>
	</tr>
	<tr style="background-color:#FFFFFF;">
		<td style="width:120px;">pic</td>
		<td><input type="file" name="pic" id="pic" style="width:200px;"></td>
	</tr>
	<tr style="background-color:#FFFFFF;">
		<td style="width:120px;">链接</td>
		<td><input type="text" name="link" id="link" style="width:200px;"></td>
	</tr>
	<br/>
	<tr style="background-color:#FFFFFF;" style="">
		<td></td>
		<td><input type="submit" value="提交">&nbsp;&nbsp;<input type="button" value="取消" onclick=\'$( "#dialog" ).dialog( "close" )\'></td>
	</tr>
	</table>
	</form>
	</div>		
	';
} elseif($operation == 'add') {
	$path="static/image/postsimg/"; //上传路径 

	$type=array("jpg","gif","bmp","jpeg","png");
	if(!in_array(strtolower(substr(strrchr($_FILES['pic']['name'], '.'), 1)),$type))
	{
	        $text=implode(",",$type);
	        echo "您只能上传以下类型文件: ",$text,"<br>";
	} 
	if($_FILES["pic"]["name"]) 
	{ 
		$file1=$_FILES["pic"]["name"]; 
		$file2 = $path.time().$file1; 
		$flag=1; 
	}
	if($flag) $result = move_uploaded_file($_FILES["pic"]["tmp_name"],$file2); 
	if($result) 
	{
		$titile = addslashes($_POST['title']);
		$link = addslashes($_POST['link']);
		$tm = date("Y-m-d H:i:s");
		$inertid = DB::insert('f_imgposts', array( 'title'=>$titile,'pic'=>$file2,'link'=>$link,'tm'=>$tm  ), 1);
		echo "<script language='javascript'>"; 
		echo "alert(\"添加完成！\");"; 
		echo "location.href='admin.php?action=imgposts';"; 
		echo "</script>"; 
	}else {
		echo '上传失败';
		print_r( $result );		
	}
} elseif($operation == 'remove') {
		DB::delete('f_imgposts', array( 'id'=>(int)$_GET['id'] ));
		echo "<script language='javascript'>"; 
		echo "alert(\"删除完成！\");"; 
		echo "location.href='admin.php?action=imgposts';"; 
		echo "</script>"; 
}
?>